﻿using Loli.Library.Hook;
using Loli.Library.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GameRobot
{
    public partial class MainForm : Form
    {

        Loli.Library.WinIo.Action _Action = new Loli.Library.WinIo.Action();


        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            //启动以后居右
            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, 10);
            base.OnLoad(e);
            //miDebugMode.Checked = this._DataCenter.Config.IsDebug;

            //时间比对

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            _Action.Shutdown();
        }

        private void butStart_Click(object sender, EventArgs e)
        {

           

            butStart.Enabled = false;
            _Action.Initialize();

            Task task = new Task(delegate()
            {
                this.SendMessage("开始运行~");
                //获取窗口句柄
                IntPtr handle = WinAPI.FindWindow(null, "E.O.S");
                this.SendMessage("窗口句柄为{0}", handle);


                WinAPI.Rect rect = new WinAPI.Rect();
                WinAPI.GetWindowRect(handle, ref rect);
                Thread.Sleep(200);
                WinAPI.SetForegroundWindow(handle);
                Thread.Sleep(200);
                int width = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;

                int dw = (width - 1024) / 2;
                int dh = height - 768 - dw;

                int xh = 29 + dh;
                int xw1 = 410 + dw;
                int xw2 = 410 + 195 + dw;

                Random rd = new Random();

                while (true)
                {
                  
                    _Action.KeyDown(Loli.Library.WinIo.KeyCodes.W);
                    Thread.Sleep(200);
                    _Action.KeyUp(Loli.Library.WinIo.KeyCodes.W);
                    Thread.Sleep(200);
                    //随机选择一个方向
                    int i = rd.Next(1, 5);
                    switch (i)
                    {
                        case 1:
                            _Action.KeyPress(Loli.Library.WinIo.KeyCodes.W, 50);
                            break;
                        case 2:
                            _Action.KeyPress(Loli.Library.WinIo.KeyCodes.S, 50);
                            break;
                        case 3:
                            _Action.KeyPress(Loli.Library.WinIo.KeyCodes.A, 50);
                            break;
                        case 4:
                            _Action.KeyPress(Loli.Library.WinIo.KeyCodes.D, 50);
                            break;
                        default:
                            break;
                    }
                    Thread.Sleep(200);
                    this.SendMessage("TAB找怪");
                    Thread.Sleep(5);
                    _Action.KeyPress(Loli.Library.WinIo.KeyCodes.Tab, 50);
                    Thread.Sleep(200);
                    //判断是否找到怪了
                    this.SendMessage("截图");
                    //Bitmap bitmap = ScreenCapturer.Capture(handle, new Point(0, 0), new Size(1024 + dw, 50 + xh));
                    ////计算窗口位移



                    //if (bitmap.GetPixel(xw1, xh).R > 180 && bitmap.GetPixel(xw2, xh).R > 180)
                    //{
                    //    this.SendMessage("发现怪物");
                    //}
                    //else
                    //{
                    //    this.SendMessage("未发现怪物");

                    //}


                }

            });

            task.Start();
        }
        //TAB,选怪，如果有，那么111
        //如果怪死亡，出现F，那么狂点F
        //然后TAB找怪，如果没有，那么视角旋转
        //如果在没有，那么向前走

        //vmhqxfpcjv----gh0d67pw


        private void SendMessage(string message, params object[] objs)
        {

            string datetime = DateTime.Now.ToString("hh:mm:ss");

            this.Invoke((UpdateInterface)delegate()
            {
                tbMessage.AppendText(datetime + string.Format(message, objs) + "\r\n");
            });
        }
    }
}
